package com.qiantu.phone.manager;

import android.content.Context;
import android.view.View;

import androidx.lifecycle.LifecycleOwner;

import com.hjq.base.BaseDialog;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.db.LLDeviceActionDBManager;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.entity.SceneInterruptModeBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.dialog.SceneIntentDialog;
import com.qiantu.phone.ui.fragment.SceneListFragment;
import com.qiantu.phone.ui.shotview.ActChangeListener;
import com.qiantu.phone.ui.shotview.BaseDeviceActView;
import com.qiantu.phone.ui.shotview.DeviceShotActBean;
import com.qiantu.phone.widget.SimpleAnimationDraweeView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import okhttp3.Call;

public class SceneOperateManager {

    private Context mContext;
    private String sceneSerialNo;
    public InterruptListener mListener;

    public SceneOperateManager(Context context){
        mContext = context;
    }

    public void onClick(View v, String sceneSerialNo){
        this.sceneSerialNo = sceneSerialNo;
        if(mClickRunnable == null){
            mClickRunnable = new ClickRunnable();
        }else{
            v.removeCallbacks(mClickRunnable);
        }
        mClickRunnable.setView(v);
        v.postDelayed(mClickRunnable, 290);
        clickCount++;
    }

    private ClickRunnable mClickRunnable;
    private int clickCount;
    private class ClickRunnable implements Runnable {

        private View view;

        public void setView(View view) {
            this.view = view;
        }

        @Override
        public void run() {
            List<SceneBean> data = LLSceneDBManager.getInstance(mContext).getScenesIntentBySerialNo(sceneSerialNo);
            switch(clickCount){
                case 1:
                    SimpleAnimationDraweeView iconScene = view.findViewById(R.id.icon_scene);
                    iconScene.setParentClickView(view);
                    iconScene.startAnimation();
                    String userSerialNo = "";
                    if(AppApplication.getInstance().getLoginInfo() != null
                            && AppApplication.getInstance().getLoginInfo().getUserInfo() != null
                            && AppApplication.getInstance().getLoginInfo().getUserInfo().getUserSerialNo() != null){
                        userSerialNo = AppApplication.getInstance().getLoginInfo().getUserInfo().getUserSerialNo();
                    }
                    SceneBean privateScene = LLSceneDBManager.getInstance(mContext).getScenesPrivateBySerialNo(sceneSerialNo, userSerialNo);
                    LLHttpManager.sceneOperation((LifecycleOwner) mContext, privateScene == null ? sceneSerialNo : privateScene.getSceneSerialNo(), new HttpCallback<HttpData<Void>>(null) {
                        @Override
                        public void onStart(Call call) {

                        }

                        @Override
                        public void onSucceed(HttpData<Void> result) {
                            List<SceneBean> sceneBeans = LLSceneDBManager.getInstance(mContext).getAll();
                            try {
                                Boolean isInterrupt = false;
                                for(SceneBean bean : sceneBeans){
                                    if(bean.getActionSettingExpireTime() == null || bean.getActionSettingExpireTime().equals("")){
                                        continue;
                                    }
                                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    Date date = format.parse(bean.getActionSettingExpireTime());
                                    Date curDate = new Date(System.currentTimeMillis());
                                    if(date.getTime() - curDate.getTime() > 0){
                                        isInterrupt = true;
                                        break;
                                    }
                                }
                                if(!isInterrupt){
                                    getInterrupData();
                                }
                            }catch (ParseException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFail(Exception e) {
                            ToastUtils.show(R.string.exec_failure);
                        }

                        @Override
                        public void onEnd(Call call) {
                            iconScene.setCanStop(true);
                        }
                    });
                    break;
                case 2:
                    if(data != null && data.size() > 0){
                        showSceneIntentDialog(sceneSerialNo, data);
                    }
                    break;
            }
            clickCount = 0;
        }
    }

    private void getInterrupData(){
        LLHttpManager.getInterrupData((LifecycleOwner)mContext, sceneSerialNo, new HttpCallback<HttpData<SceneInterruptModeBean>>(null){
            @Override
            public void onSucceed(HttpData<SceneInterruptModeBean> result) {
                super.onSucceed(result);
                SceneInterruptModeBean sceneInterruptModeBean = result.getData();
                if(mListener != null && sceneInterruptModeBean.getEnable()){
                    mListener.updateInterrupt(sceneInterruptModeBean.getRunTimeSec());
                }

            }
        });
    }

    public void setInterrupListener(InterruptListener listener) {
        mListener = listener;
    }

    private BaseDialog mSceneIntentDialog;
    private SceneIntentDialog.Builder SceneIntentDialogBuilder;
    private void showSceneIntentDialog(String sceneSerialNo, List<SceneBean> data) {
        if (mSceneIntentDialog == null) {
            SceneIntentDialogBuilder = new SceneIntentDialog.Builder(mContext);
            mSceneIntentDialog = SceneIntentDialogBuilder.create();
        }
        SceneIntentDialogBuilder.setData(sceneSerialNo, data);
        mSceneIntentDialog.show();
    }

    public interface InterruptListener {
        void updateInterrupt(int time);
    }
}
